var twbStoreFavouredPricevm = new Vue({
    el: '#app',
    data: {
        tableData: [],//表格当前页数据
        pagesize: 20,//默认每页数据量
        storeFavouredPrice: {}, //当前选择行数据
        currentPage: 1,//当前页码
        totalCount: 0,//默认数据总数
        multipleSelection: [],//多选数组
        q: {//搜索条件
            favouredName: '',
        },
        dialogFormVisible: false,
        title: '',
        labelPosition: 'right',
    },
    created() {
        Vue.nextTick(function () {
            //载入数据
            twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage, twbStoreFavouredPricevm.pagesize);
        });
    },
    methods: {
        //从后台获取数据
        loadData: function (page, limit) {
            var params = {page: page, limit: limit, favouredName: twbStoreFavouredPricevm.q.favouredName}
            $.ajax({
                type: "POST",
                url: baseURL + 'storeFavouredPrice/storefavouredprice/twblist',
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code === 0) {
                        twbStoreFavouredPricevm.tableData = r.page.list
                        twbStoreFavouredPricevm.totalCount = r.page.totalCount
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //多选响应
        handleSelectionChange: function (val) {
            // twbStoreFavouredPricevm.multipleSelection = val;
            // console.log(twbStoreFavouredPricevm.multipleSelection)
        },
        //点击行响应
        handleclick: function (row, event, column) {
            // twbStoreFavouredPricevm.storeMember = JSON.parse(JSON.stringify(row));//深克隆
        },
        //弹出详情框
        showDetail(row) {
            twbStoreFavouredPricevm.title = "详情";
            twbStoreFavouredPricevm.storeFavouredPrice = row
            twbStoreFavouredPricevm.dialogFormVisible = true
        },
        //弹出新增框
        add() {
            twbStoreFavouredPricevm.title = "新增";
            twbStoreFavouredPricevm.dialogFormVisible = true
            twbStoreFavouredPricevm.storeFavouredPrice = {hangsAccountCustomer: 0};
        },
        //弹出编辑框
        update: function (index, row) {
            twbStoreFavouredPricevm.title = '编辑'
            twbStoreFavouredPricevm.storeFavouredPrice = JSON.parse(JSON.stringify(row));//深克隆
            twbStoreFavouredPricevm.dialogFormVisible = true
        },
        saveOrUpdate: function (formName) {
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    if (twbStoreFavouredPricevm.storeFavouredPrice.discountRate <= 0 || twbStoreFavouredPricevm.storeFavouredPrice.discountRate >= 1) {
                        alert('折扣率有效范围:0-1之间')
                        return
                    }
                    twbStoreFavouredPricevm.storeFavouredPrice.startTime = twbStoreFavouredPricevm.dateFormat(new Date(twbStoreFavouredPricevm.storeFavouredPrice.startTime))
                    twbStoreFavouredPricevm.storeFavouredPrice.endTime = twbStoreFavouredPricevm.dateFormat(new Date(twbStoreFavouredPricevm.storeFavouredPrice.endTime))
                    if (twbStoreFavouredPricevm.storeFavouredPrice.startTime >= twbStoreFavouredPricevm.storeFavouredPrice.endTime) {
                        alert('开始时间必须小于结束时间')
                        return
                    }
                    var url = twbStoreFavouredPricevm.storeFavouredPrice.favouredId == null ? "storeFavouredPrice/storefavouredprice/twbsave" : "storeFavouredPrice/storefavouredprice/twbupdate";
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(twbStoreFavouredPricevm.storeFavouredPrice),
                        success: function (r) {
                            if (r.code === 0) {
                                twbStoreFavouredPricevm.$alert('操作成功!', '提示', {
                                    confirmButtonText: '确定',
                                    callback: function (action) {
                                        twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage = 1, twbStoreFavouredPricevm.pagesize);//刷新后回到首页
                                        twbStoreFavouredPricevm.$refs[formName].resetFields();
                                        twbStoreFavouredPricevm.dialogFormVisible = false
                                    }
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //删除恢复
        del: function (index, row) {
            var parmas=[row.favouredId]
            twbStoreFavouredPricevm.$alert(row.delFlag? '确定恢复?' : '确定删除?', '提示', {
                confirmButtonText: '确定',
                callback: function (action) {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "storeFavouredPrice/storefavouredprice/delete",
                        contentType: "application/json",
                        data: JSON.stringify(parmas),
                        success: function (r) {
                            if (r.code === 0) {
                                twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage, twbStoreFavouredPricevm.pagesize);//刷新后页面在当前页
                                alert('删除成功')
                                //twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage=1, twbStoreFavouredPricevm.pagesize);刷新后页面回到首页
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }
            });
        },
        //搜索
        search: function () {
            twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage, twbStoreFavouredPricevm.pagesize);
        },
        //重置
        rest() {
            twbStoreFavouredPricevm.q.favouredName = ''
            twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage = 1, twbStoreFavouredPricevm.pagesize);
        },
        //每页显示数据量变更
        handleSizeChange: function (val) {
            twbStoreFavouredPricevm.pagesize = val;
            twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage, twbStoreFavouredPricevm.pagesize);
        },
        //页码变更
        handleCurrentChange: function (val) {
            twbStoreFavouredPricevm.currentPage = val;
            twbStoreFavouredPricevm.loadData(twbStoreFavouredPricevm.currentPage, twbStoreFavouredPricevm.pagesize);
        },
        /* 格式化日期yyyy-MM-dd HH:mm:ss */
        dateFormat(date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        },
    }
});
